package uranium.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.JTabbedPane;
import java.awt.GridBagConstraints;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.view.JasperViewer;

import uranium.combo.ComboBoxAutoComplete;
import uranium.combo.MyComboClie;
import uranium.combo.MyComboTelN;
import uranium.combo.MyComboTelU;
import uranium.db.ConnectionManager;
import uranium.db.DBManager;
import uranium.gui.add.AddNewClienteGUI;
import uranium.gui.add.AddNewFornGUI;
import uranium.utility.ControlloDati;

import java.awt.Rectangle;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.Vector;
import com.toedter.components.JSpinField;
import uranium.combo.MyComboProd;
import uranium.combo.MyComboSkRic;

public class VenditaGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="193,25"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel jPanel1 = null;
	private JPanel jPanel2 = null;
	private JPanel jPanel3 = null;
	private JPanel jPanel4 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField1 = null;
	private JLabel jLabel3 = null;
	private JComboBox jComboBox1 = null;
	private JButton jButton = null;
	private JLabel jLabel4 = null;
	private JComboBox jComboBox2 = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JTextField jTextField2 = null;
	private JTextField jTextField11 = null;
	private JComboBox jComboBox11 = null;
	private JComboBox jComboBox12 = null;
	private JButton jButton3 = null;
	private JButton jButton11 = null;
	private JButton jButton21 = null;
	private JLabel jLabel5 = null;
	private JLabel jLabel11 = null;
	private JLabel jLabel21 = null;
	private JLabel jLabel31 = null;
	private JLabel jLabel41 = null;
	private JLabel jLabel51 = null;
	private JLabel jLabel111 = null;
	private JLabel jLabel211 = null;
	private JLabel jLabel411 = null;
	private JTextField jTextField4 = null;
	private JTextField jTextField5 = null;
	private JButton jButton111 = null;
	private JButton jButton211 = null;
	private JLabel jLabel6 = null;
	private JTextField jTextField8 = null;
	private JLabel jLabel511 = null;
	private JLabel jLabel1111 = null;
	private JLabel jLabel2111 = null;
	private JLabel jLabel4111 = null;
	private JLabel jLabel61 = null;
	private JTextField jTextField41 = null;
	private JTextField jTextField42 = null;
	private JTextField jTextField311 = null;
	private JButton jButton1111 = null;
	private JButton jButton2111 = null;
	private Vector<String> v = null;  //  @jve:decl-index=0:
	private DBManager dbm = null;
	private Connection connessione;
	private Statement st = null;
	private ResultSet rs = null;  //  @jve:decl-index=0:
	private ConnectionManager cm;
	private int qtaskric;
	private int qtacanc;
	private JSpinField jSpinField = null;
	private JSpinField jSpinField1 = null;
	private JTextField jTextFieldNT = null;
	private JButton jButtonCercaTN = null;
	private double prezzoin = 0.0;
	private JTextField jTextFieldVT = null;
	private JButton jButtonTV = null;
	private JTextField jTextFieldSR = null;
	private JButton jButtonCercaSR = null;
	private JTextField jTextFieldVP = null;
	private JButton jButtonCercaP = null;
	private JButton jButtonStampa = null;
	private JButton jButtonStampa2 = null;
	private int id = 0;
	private int ric = 0;
	
	public VenditaGUI(DBManager dbm, JFrame padre, ConnectionManager cm){
		super(padre, false);
		this.jFrame = padre;
		this.dbm = dbm;
		this.cm = cm;
		this.connessione = cm.getConnection();
		try{
			st = connessione.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException e39){

			e39.printStackTrace();
		}
		initialize();
		
	}
	
	private void initialize(){
		this.setSize(new Dimension(530, 377));
		this.setTitle("Nuova Vendita");
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}
	
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJTabbedPane(), gridBagConstraints);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setToolTipText("");
			jTabbedPane.setName("");
			jTabbedPane.addTab("Telefono Nuovo", null, getJPanel1(), null);
			jTabbedPane.addTab("Telefono Usato", null, getJPanel2(), null);
			jTabbedPane.addTab("Schede/Ricariche", null, getJPanel3(), null);
			jTabbedPane.addTab("Prodotti", null, getJPanel4(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Pagamento");
			jLabel4.setBounds(new Rectangle(97, 136, 69, 16));
			jLabel3 = new JLabel();
			jLabel3.setText("Cliente");
			jLabel3.setBounds(new Rectangle(121, 109, 44, 16));
			jLabel2 = new JLabel();
			jLabel2.setText("Prezzo OUT");
			jLabel2.setBounds(new Rectangle(94, 85, 72, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("Modello");
			jLabel1.setBounds(new Rectangle(106, 60, 59, 16));
			jLabel = new JLabel();
			jLabel.setText("Telefono");
			jLabel.setBounds(new Rectangle(108, 33, 55, 16));
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(jLabel, null);
			
			jPanel1.add(jLabel1, null);
			jPanel1.add(getJTextField(), null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getJTextField1(), null);
			jPanel1.add(jLabel3, null);
			jPanel1.add(getJComboBox1(), null);
			jPanel1.add(getJButton(), null);
			jPanel1.add(jLabel4, null);
			jPanel1.add(getJComboBox2(), null);
			jPanel1.add(getJButton1(), null);
			jPanel1.add(getJButton2(), null);
			jPanel1.add(getJTextFieldNT(), null);
			jPanel1.add(getJButtonCercaTN(), null);
			jPanel1.add(getJButtonStampa(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jLabel41 = new JLabel();
			jLabel41.setBounds(new Rectangle(97, 136, 69, 16));
			jLabel41.setText("Pagamento");
			jLabel31 = new JLabel();
			jLabel31.setBounds(new Rectangle(121, 109, 44, 16));
			jLabel31.setText("Cliente");
			jLabel21 = new JLabel();
			jLabel21.setBounds(new Rectangle(94, 85, 72, 16));
			jLabel21.setText("Prezzo OUT");
			jLabel11 = new JLabel();
			jLabel11.setBounds(new Rectangle(106, 60, 59, 16));
			jLabel11.setText("Modello");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(108, 33, 55, 16));
			jLabel5.setText("Telefono");
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			
			jPanel2.add(getJTextField2(), null);
			jPanel2.add(getJTextField11(), null);
			jPanel2.add(getJComboBox11(), null);
			jPanel2.add(getJComboBox12(), null);
			jPanel2.add(getJButton3(), null);
			jPanel2.add(getJButton11(), null);
			jPanel2.add(getJButton21(), null);
			jPanel2.add(jLabel5, null);
			jPanel2.add(jLabel11, null);
			jPanel2.add(jLabel21, null);
			jPanel2.add(jLabel31, null);
			jPanel2.add(jLabel41, null);
			jPanel2.add(getJTextFieldVT(), null);
			jPanel2.add(getJButtonTV(), null);
			jPanel2.add(getJButtonStampa2(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(120, 125, 46, 16));
			jLabel6.setText("Totale");
			jLabel411 = new JLabel();
			jLabel411.setBounds(new Rectangle(111, 103, 56, 16));
			jLabel411.setText("Quantita'");
			jLabel211 = new JLabel();
			jLabel211.setBounds(new Rectangle(94, 81, 72, 16));
			jLabel211.setText("Prezzo OUT");
			jLabel111 = new JLabel();
			jLabel111.setBounds(new Rectangle(98, 59, 68, 16));
			jLabel111.setText("Descrizione");
			jLabel51 = new JLabel();
			jLabel51.setBounds(new Rectangle(119, 37, 47, 16));
			jLabel51.setText("Codice");
			jPanel3 = new JPanel();
			jPanel3.setLayout(null);
			jPanel3.add(jLabel51, null);
			jPanel3.add(jLabel111, null);
			jPanel3.add(jLabel211, null);
			jPanel3.add(jLabel411, null);
			jPanel3.add(getJTextField4(), null);
			jPanel3.add(getJTextField5(), null);
			jPanel3.add(getJButton111(), null);
			jPanel3.add(getJButton211(), null);
			jPanel3.add(jLabel6, null);
			jPanel3.add(getJTextField8(), null);
			jPanel3.add(getJSpinField(), null);
			jPanel3.add(getJTextFieldSR(), null);
			jPanel3.add(getJButtonCercaSR(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			jLabel61 = new JLabel();
			jLabel61.setBounds(new Rectangle(120, 125, 46, 16));
			jLabel61.setText("Totale");
			jLabel4111 = new JLabel();
			jLabel4111.setBounds(new Rectangle(111, 103, 56, 16));
			jLabel4111.setText("Quantita'");
			jLabel2111 = new JLabel();
			jLabel2111.setBounds(new Rectangle(94, 81, 72, 16));
			jLabel2111.setText("Prezzo OUT");
			jLabel1111 = new JLabel();
			jLabel1111.setBounds(new Rectangle(98, 59, 68, 16));
			jLabel1111.setText("Descrizione");
			jLabel511 = new JLabel();
			jLabel511.setBounds(new Rectangle(119, 37, 47, 16));
			jLabel511.setText("Codice");
			jPanel4 = new JPanel();
			jPanel4.setLayout(null);
			jPanel4.add(jLabel511, null);
			jPanel4.add(jLabel1111, null);
			jPanel4.add(jLabel2111, null);
			jPanel4.add(jLabel4111, null);
			jPanel4.add(jLabel61, null);
			jPanel4.add(getJTextField41(), null);
			jPanel4.add(getJTextField42(), null);
			jPanel4.add(getJTextField311(), null);
			jPanel4.add(getJButton1111(), null);
			jPanel4.add(getJButton2111(), null);
			jPanel4.add(getJSpinField1(), null);
			jPanel4.add(getJTextFieldVP(), null);
			jPanel4.add(getJButtonCercaP(), null);
		}
		return jPanel4;
	}

	private void aggiornaCombo(){
		if(!(jTextFieldNT.getText().trim().equals(""))){
			//int index = ((String)jComboBox.getSelectedItem()).indexOf("-");
			String imei = jTextFieldNT.getText();
			String query = "SELECT modello,prezzoin,prezzoout FROM telefonin WHERE imei='"+imei+"'";
			try{
				rs = st.executeQuery(query);
				if (rs.next()){				
					jTextField.setText(String.valueOf(rs.getString(1)));
					prezzoin = rs.getDouble(2);
					jTextField1.setText(String.valueOf(rs.getDouble(3)));
					ric = 1;
				}
				else{
					JOptionPane.showMessageDialog(jFrame, "Codice non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					jTextFieldNT.setText("");
				}
			}catch (Exception e1){
				e1.printStackTrace();
			}
		}
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setLocation(new Point(184, 56));
			jTextField.setSize(new Dimension(180, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setLocation(new Point(184, 78));
			jTextField1.setSize(new Dimension(180, 22));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	/*private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			jComboBox1 = new MyComboClie(dbm);
			jComboBox1.stateChange();
			jComboBox1.setBounds(new Rectangle(184, 100, 180, 27));
		}
		return jComboBox1;
	}*/
	
	private JComboBox getJComboBox1(){
		if (jComboBox1 == null){
			try{
				LinkedList lista = (LinkedList) dbm.getClienti();
	            jComboBox1 = new JComboBox();
	            caricaComboBox(jComboBox1, lista.toArray());
	            jComboBox1.setEditable(true);
	            jComboBox1.setBounds(new Rectangle(184, 100, 180, 27));
	            // cmbListaPortate.addKeyListener(new MyKeyListeners());
	            JTextComponent editor = (JTextComponent)jComboBox1.getEditor().getEditorComponent();
	            //editor.addKeyListener(new MyKeyListeners());
	            new ComboBoxAutoComplete(jComboBox1);

			}catch (java.lang.Throwable e){
	               // TODO: Something
			}
		}
		return jComboBox1;
	}
	
	private void caricaComboBox(JComboBox combo, Object[] o){
		combo.removeAllItems();
		combo.addItem("");
		for (int i = 0; i < o.length; i++){
			combo.addItem(o[i]);
		}
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Nuovo");
			jButton.setBounds(new Rectangle(375, 100, 75, 29));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						apriAddNewClienteGUI();
						//jComboBox1.stateChange();
						//jComboBox11.stateChange();
					}
					catch(Exception e37){
						e37.printStackTrace();
					}
				}
			});
		}
		return jButton;
	}
	
	private void apriAddNewClienteGUI(){
		//final AddNewCancGUI padre = this;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				AddNewClienteGUI cerca = new AddNewClienteGUI(dbm, jFrame);
	            
				LinkedList lista = (LinkedList) dbm.getClienti();
	            caricaComboBox(jComboBox1, lista.toArray());
	            caricaComboBox(jComboBox11, lista.toArray());
			}
		});

	}

	/**
	 * This method initializes jComboBox2	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox2() {
		if (jComboBox2 == null) {
			v = new Vector<String>();
			v.add("SI");
			v.add("NO");
			jComboBox2 = new JComboBox(v);
			jComboBox2.setLocation(new Point(184, 129));
			jComboBox2.setSize(new Dimension(180, 27));
		}
		return jComboBox2;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Registra Vendita");
			jButton1.setLocation(new Point(195, 200));
			jButton1.setSize(new Dimension(130, 29));
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						boolean er = vendTelN();
						if(!er)
							close();
					}
					catch(Exception e42){
						e42.printStackTrace();
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Annulla");
			jButton2.setSize(new Dimension(130, 29));
			jButton2.setLocation(new Point(25, 200));
			jButton2.setPreferredSize(new Dimension(130, 29));
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e36){
						e36.printStackTrace();
					}
				}
			});
		}
		return jButton2;
	}
	
	private void close(){
		this.dispose();
	}

	private void aggiornaCombo1(){
		if(!(jTextFieldVT.getText().trim().equals(""))){
			String ime = jTextFieldVT.getText();
			String q = "SELECT modello,prezzoin,prezzoout FROM telefoniu WHERE imei='"+ime+"'";
			try{
				rs = st.executeQuery(q);
				if (rs.next()){
					jTextField2.setText(String.valueOf(rs.getString(1)));
					prezzoin = rs.getDouble(2);
					jTextField11.setText(String.valueOf(rs.getDouble(3)));
					ric = 1;
				}
				else{
					JOptionPane.showMessageDialog(jFrame, "Codice non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					jTextFieldVT.setText("");
				}
			}catch (Exception e1){
				e1.printStackTrace();
			}
		}
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(184, 56, 180, 22));
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField11	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField11() {
		if (jTextField11 == null) {
			jTextField11 = new JTextField();
			jTextField11.setBounds(new Rectangle(184, 78, 180, 22));
		}
		return jTextField11;
	}

	/**
	 * This method initializes jComboBox11	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	/*private JComboBox getJComboBox11() {
		if (jComboBox11 == null) {
			jComboBox11 = new MyComboClie(dbm);
			jComboBox11.stateChange();
			jComboBox11.setBounds(new Rectangle(184, 100, 180, 27));
		}
		return jComboBox11;
	}*/
	
	private JComboBox getJComboBox11(){
		if (jComboBox11 == null){
			try{
				LinkedList lista = (LinkedList) dbm.getClienti();
	            jComboBox11 = new JComboBox();
	            caricaComboBox(jComboBox11, lista.toArray());
	            jComboBox11.setEditable(true);
	            jComboBox11.setBounds(new Rectangle(184, 100, 180, 27));
	            // cmbListaPortate.addKeyListener(new MyKeyListeners());
	            //JTextComponent editor = (JTextComponent)jComboBox.getEditor().getEditorComponent();
	            //editor.addKeyListener(new MyKeyListeners());
	            new ComboBoxAutoComplete(jComboBox11);

			}catch (java.lang.Throwable e){
	               // TODO: Something
			}
		}
		return jComboBox11;
	}

	/**
	 * This method initializes jComboBox12	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox12() {
		if (jComboBox12 == null) {
			jComboBox12 = new JComboBox(v);
			jComboBox12.setBounds(new Rectangle(184, 129, 180, 27));
		}
		return jComboBox12;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(375, 100, 75, 29));
			jButton3.setText("Nuovo");
			jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						apriAddNewClienteGUI();
						//jComboBox11.stateChange();
						//jComboBox1.stateChange();
					}
					catch(Exception e38){
						e38.printStackTrace();
					}
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton11	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton11() {
		if (jButton11 == null) {
			jButton11 = new JButton();
			jButton11.setBounds(new Rectangle(195, 200, 130, 29));
			jButton11.setText("Registra Vendita");
			jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						boolean er = vendTelU();
						if(!er)
							close();
					}
					catch(Exception e41){
						e41.printStackTrace();
					}
				}
			});
		}
		return jButton11;
	}

	/**
	 * This method initializes jButton21	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton21() {
		if (jButton21 == null) {
			jButton21 = new JButton();
			jButton21.setBounds(new Rectangle(25, 200, 130, 29));
			jButton21.setText("Annulla");
			jButton21.setPreferredSize(new Dimension(130, 29));
			jButton21.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e37){
						e37.printStackTrace();
					}
				}
			});
		}
		return jButton21;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setBounds(new Rectangle(184, 56, 180, 22));
		}
		return jTextField4;
	}

	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField5() {
		if (jTextField5 == null) {
			jTextField5 = new JTextField();
			jTextField5.setBounds(new Rectangle(184, 78, 180, 22));
		}
		return jTextField5;
	}

	/**
	 * This method initializes jButton111	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton111() {
		if (jButton111 == null) {
			jButton111 = new JButton();
			jButton111.setBounds(new Rectangle(214, 200, 130, 29));
			jButton111.setText("Registra Vendita");
			jButton111.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						vendSkRic();
					}
					catch(Exception e43){
						e43.printStackTrace();
					}
				}
			});
		}
		return jButton111;
	}

	/**
	 * This method initializes jButton211	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton211() {
		if (jButton211 == null) {
			jButton211 = new JButton();
			jButton211.setBounds(new Rectangle(214, 250, 130, 29));
			jButton211.setText("Annulla");
			jButton211.setPreferredSize(new Dimension(130, 29));
			jButton211.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e38){
						e38.printStackTrace();
					}
				}
			});
		}
		return jButton211;
	}

	/**
	 * This method initializes jTextField8	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField8() {
		if (jTextField8 == null) {
			jTextField8 = new JTextField();
			jTextField8.setBounds(new Rectangle(184, 122, 180, 22));
		}
		return jTextField8;
	}

	/**
	 * This method initializes jTextField41	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField41() {
		if (jTextField41 == null) {
			jTextField41 = new JTextField();
			jTextField41.setBounds(new Rectangle(184, 56, 180, 22));
		}
		return jTextField41;
	}

	/**
	 * This method initializes jTextField42	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField42() {
		if (jTextField42 == null) {
			jTextField42 = new JTextField();
			jTextField42.setBounds(new Rectangle(184, 78, 180, 22));
		}
		return jTextField42;
	}

	/**
	 * This method initializes jTextField311	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField311() {
		if (jTextField311 == null) {
			jTextField311 = new JTextField();
			jTextField311.setBounds(new Rectangle(184, 122, 180, 22));
		}
		return jTextField311;
	}

	/**
	 * This method initializes jButton1111	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1111() {
		if (jButton1111 == null) {
			jButton1111 = new JButton();
			jButton1111.setBounds(new Rectangle(214, 200, 130, 29));
			jButton1111.setText("Registra Vendita");
			jButton1111.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						vendCanc();
					}
					catch(Exception e44){
						e44.printStackTrace();
					}
				}
			});
		}
		return jButton1111;
	}

	/**
	 * This method initializes jButton2111	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2111() {
		if (jButton2111 == null) {
			jButton2111 = new JButton();
			jButton2111.setBounds(new Rectangle(214, 250, 130, 29));
			jButton2111.setText("Annulla");
			jButton2111.setPreferredSize(new Dimension(130, 29));
			jButton2111.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e39){
						e39.printStackTrace();
					}
				}
			});
		}
		return jButton2111;
	}

	private void cercaSR(){
		if(!(jTextFieldSR.getText().trim().equals(""))){
			String cbarre = jTextFieldSR.getText();
		
			String query = "SELECT id,descrizione, qta, prezzoin, prezzoout FROM skeric WHERE cbarre='"+cbarre+"'";
			try{
				rs = st.executeQuery(query);
				if(!rs.next()){
					JOptionPane.showMessageDialog(jFrame, "Codice non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					jTextFieldSR.setText("");
				}
				else{
					id = rs.getInt(1);
					jTextField4.setText((String)rs.getString(2));
					qtaskric = rs.getInt(3);
					prezzoin = rs.getDouble(4);
					jTextField5.setText((String)rs.getString(5));
					ric = 1;
				}
			}
			catch(Exception e40){
				e40.printStackTrace();
			}
		}
		
	}
	
	private void cercaC(){
		if(!(jTextFieldVP.getText().trim().equals(""))){
			String cbarre = jTextFieldVP.getText();
			String query = "SELECT id, descrizione, qta, prezzoin, prezzoout FROM cancelleria WHERE cbarre='"+cbarre+"'";
			try{
				rs = st.executeQuery(query);
				if(!rs.next()){
					JOptionPane.showMessageDialog(jFrame, "Codice non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					jTextFieldVP.setText("");
				}
				else{
					id = rs.getInt(1);
					jTextField41.setText((String)rs.getString(2));
					qtacanc = rs.getInt(3);
					prezzoin = rs.getDouble(4);
					jTextField42.setText((String)rs.getString(5));
					ric = 1;
				}
			}
			catch(Exception e47){
				e47.printStackTrace();
			}
		}
	}
	
	private synchronized boolean vendTelU(){
		boolean error = false;
		if(jTextFieldVT.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Telefono non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField2.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Modello non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField11.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jComboBox11.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo Cliente non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(jComboBox12.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo Pagamento non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			String insert = null;
			String codice = jTextFieldVT.getText();
			String modello = jTextField2.getText();
			//int indclie = ((String)jComboBox11.getSelectedItem()).indexOf(" ", 1);
			String cliente = (String)jComboBox11.getSelectedItem();//).substring(indclie+1, ((String)jComboBox11.getSelectedItem()).length());
			
			int qta = 1;
			//prezzoin = Double.parseDouble(jTextField2.getText());
			double prezzout = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField11.getText()));
			double guadagno = prezzout - prezzoin;
			Date d = new Date();
			Calendar c = new GregorianCalendar();
			c.setTime(d);
			int g = -1, m = -1, a = -1;
			g = c.get(Calendar.DAY_OF_MONTH);
			m = (c.get(Calendar.MONTH)) + 1;
			a = c.get(Calendar.YEAR);
			String data = String.valueOf(a)+"-"+String.valueOf(m)+"-"+String.valueOf(g);
			
			if ((String)jComboBox12.getSelectedItem() == "SI") 
				insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"','"+cliente+"')";
			
			else 
				insert = "INSERT INTO sospesi (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"','"+cliente+"')";
			
			/*String q = "SELECT qta FROM vendite WHERE codice='"+codice+"'";
			try{
				rs = st.executeQuery(q);
				if (rs.next()){
					int nqta = rs.getInt(1)+qta;
					System.out.println(nqta);
					insert = "UPDATE vendite SET qta="+nqta+" WHERE codice='"+codice+"'";
				}
				else if ((String)jComboBox12.getSelectedItem() == "SI") insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+data+"','"+qta+"','"+cliente+"')";
						else insert = "INSERT INTO sospesi (codice,descrizione,prezzoin,prezzoout,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+data+"','"+qta+"','"+cliente+"')";
			}catch (Exception e){
				e.printStackTrace();
			}*/
			
			//stampa1();
			dbm.executeQuery(insert);
			if (ric != 0){
				update("telefoniu", "imei", codice, qta, 1);
				ric = 0;
			}
			//this.dispose();
		}
		return error;
	}
	
	private synchronized void stampa1(){
				String combo = (String)jComboBox11.getSelectedItem();
				int scelta = JOptionPane.showConfirmDialog(jFrame, "Impostare automaticamente la data?", getTitle(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if(!combo.trim().equals("")){
					int cfind = combo.lastIndexOf("-");
					int id = Integer.parseInt(combo.substring(cfind+1));
					//String cf = combo.substring(0, cfind);
					String query1 = "SELECT * FROM clienti WHERE id="+id;
					String nome = "";
					String cognome = "";
					String indirizzo = "";
					String cap = "";
					String citta = "";
					try{
						ResultSet rs = dbm.executeQuery(query1);
						while(rs.next()){
							cognome = rs.getString("cognome");
							nome = rs.getString("nome");
							indirizzo = rs.getString("indirizzo");
							cap = rs.getString("cap");
							citta = rs.getString("citta");
						}
					}
					catch(Exception ed){
						ed.printStackTrace();
					}
					String imei = jTextFieldVT.getText();
					String modello = jTextField2.getText();
					String query2 = "INSERT INTO stampa (imei,modello,cognome,nome,indirizzo,cap,citta) VALUES('"+imei+"','"+modello+"','"+cognome+"','"+nome+"','"+indirizzo+"','"+cap+"','"+citta+"')";
					dbm.executeQuery(query2);
					try{
						if(scelta == 0){
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaCD.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
						else{
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaC.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
					}
					catch(JRException ef){
						ef.printStackTrace();
					}
				}
				else{
					String nome = "";
					String cognome = "";
					String indirizzo = "";
					String cap = "";
					String citta = "";
					String imei = jTextFieldVT.getText();
					String modello = jTextField2.getText();
					String query2 = "INSERT INTO stampa (imei,modello,cognome,nome,indirizzo,cap,citta) VALUES('"+imei+"','"+modello+"','"+cognome+"','"+nome+"','"+indirizzo+"','"+cap+"','"+citta+"')";
					dbm.executeQuery(query2);
					try{
						if(scelta == 0){
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaSC.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
						else{
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaSCD.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
					}
					catch(JRException ef){
						ef.printStackTrace();
					}
				}
	}
	
	private synchronized boolean vendTelN(){
		boolean error = false;
		if(jTextFieldNT.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Telefono non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Modello non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField1.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jComboBox1.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo Cliente non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(jComboBox2.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo Pagamento non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			String insert = null;
			String codice = jTextFieldNT.getText();
			String modello = jTextField.getText();
			//int indclie = ((String)jComboBox1.getSelectedItem()).lastIndexOf("-");
			//String cliente = ((String)jComboBox1.getSelectedItem()).substring(indclie+1, ((String)jComboBox1.getSelectedItem()).length());
			String cliente = (String)jComboBox1.getSelectedItem();
			
			int qta = 1;
			//double prezzoin = Double.parseDouble(jTextField.getText());
			double prezzout = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField1.getText()));
			double guadagno = prezzout - prezzoin;
			Date d = new Date();
			Calendar c = new GregorianCalendar();
			c.setTime(d);
			int g = -1, m = -1, a = -1;
			g = c.get(Calendar.DAY_OF_MONTH);
			m = (c.get(Calendar.MONTH)) + 1;
			a = c.get(Calendar.YEAR);
			String data = String.valueOf(a)+"-"+String.valueOf(m)+"-"+String.valueOf(g);
			String pag = "NO";
			if ((String)jComboBox2.getSelectedItem() == "SI") 
				insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"','"+cliente+"')";
			
			else 
				insert = "INSERT INTO sospesi (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta,cliente,pagato) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"','"+cliente+"','"+pag+"')";
			
			/*String q = "SELECT qta FROM vendite WHERE codice='"+codice+"'";
			try{
				rs = st.executeQuery(q);
				if (rs.next()){
					int nqta = rs.getInt(1)+qta;
					insert = "UPDATE vendite SET qta="+nqta+" WHERE codice='"+codice+"'";
				}
				else if ((String)jComboBox2.getSelectedItem() == "SI") insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+data+"','"+qta+"','"+cliente+"')";
				else insert = "INSERT INTO sospesi (codice,descrizione,prezzoin,prezzoout,data,qta,cliente,pagato) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+data+"','"+qta+"','"+cliente+"','"+pag+"')";
			}catch (Exception e){
				e.printStackTrace();
			}*/
			
			//stampa();
			dbm.executeQuery(insert);
			if(ric != 0){
				update("telefonin", "imei", codice, qta, 1);
				ric = 0;
			}
			//this.dispose();
		}
		return error;
	}
	
	private synchronized void stampa(){
		int scelta = JOptionPane.showConfirmDialog(jFrame, "Impostare automaticamente la data?", getTitle(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				String combo = (String)jComboBox1.getSelectedItem();
			
				if(!combo.trim().equals("")){
					int cfind = combo.lastIndexOf("-");
					int id = Integer.parseInt(combo.substring(cfind+1));
					//String cf = combo.substring(0, cfind);
					String query1 = "SELECT * FROM clienti WHERE id="+id;
					String nome = "";
					String cognome = "";
					String indirizzo = "";
					String cap = "";
					String citta = "";
					try{
						ResultSet rs = dbm.executeQuery(query1);
						while(rs.next()){
							cognome = rs.getString("cognome");
							nome = rs.getString("nome");
							indirizzo = rs.getString("indirizzo");
							cap = rs.getString("cap");
							citta = rs.getString("citta");
						}
					}
					catch(Exception ed){
						ed.printStackTrace();
					}
					String imei = jTextFieldNT.getText();
					String modello = jTextField.getText();
					String query2 = "INSERT INTO stampa (imei,modello,cognome,nome,indirizzo,cap,citta) VALUES('"+imei+"','"+modello+"','"+cognome+"','"+nome+"','"+indirizzo+"','"+cap+"','"+citta+"')";
					dbm.executeQuery(query2);
					//int scelta = JOptionPane.showConfirmDialog(this, "Impostare automaticamente la data?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					try{
						if(scelta == 0){
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaCD.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
						else{
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaC.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
					} 
					catch (JRException e){
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else{
					String nome = "";
					String cognome = "";
					String indirizzo = "";
					String cap = "";
					String citta = "";
					String imei = jTextFieldNT.getText();
					String modello = jTextField.getText();
					String query2 = "INSERT INTO stampa (imei,modello,cognome,nome,indirizzo,cap,citta) VALUES('"+imei+"','"+modello+"','"+cognome+"','"+nome+"','"+indirizzo+"','"+cap+"','"+citta+"')";
					dbm.executeQuery(query2);
					try{
						if(scelta == 0){
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaSC.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}
						else{
							JasperPrint jasperPrint = JasperFillManager.fillReport("AttVenditaSCD.jasper", null, dbm.getConnessione());
							JasperViewer.viewReport(jasperPrint, false);
						}		
					} 
					catch (JRException e){
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
	}
	
	private synchronized void vendSkRic(){
		boolean error = false;
		if(jTextFieldSR.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Codice non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField4.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Descrizione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField5.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jTextField6.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Descrizione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(jSpinField.getValue() == 0){
			JOptionPane.showMessageDialog(jFrame, "Campo Quantita' non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField8.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Totale non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jSpinField.getValue() > qtaskric){
			JOptionPane.showMessageDialog(jFrame, "Quantita' non disponibile", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(error == false){
			String insert = null;
			String cbarre = jTextFieldSR.getText();
			String descrizione = jTextField4.getText();

			int qta = jSpinField.getValue();
			//double prezzoin = Double.parseDouble(jTextField4.getText());
			double prezzout = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField5.getText()));
			double guadagno = prezzout - prezzoin;
			Date d = new Date();
			Calendar c = new GregorianCalendar();
			c.setTime(d);
			int g = -1, m = -1, a = -1;
			g = c.get(Calendar.DAY_OF_MONTH);
			m = (c.get(Calendar.MONTH)) + 1;
			a = c.get(Calendar.YEAR);
			String data = String.valueOf(a)+"-"+String.valueOf(m)+"-"+String.valueOf(g);
			
			String q = "SELECT qta,data FROM vendite WHERE codice='"+cbarre+"'";
			try{
				rs = st.executeQuery(q);
				if (rs.next()){
					int nqta = rs.getInt(1)+qta;
					if (data.equals(rs.getString("data")))
					insert = "UPDATE vendite SET qta="+nqta+" WHERE codice='"+cbarre+"'";
					else insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta) VALUES('"+cbarre+"','"+descrizione+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"')";
				}
				else insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta) VALUES('"+cbarre+"','"+descrizione+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"')";
			}catch (Exception e){
				e.printStackTrace();
			}
			
			dbm.executeQuery(insert);
			if(ric != 0){
				update("skeric", "id", id, qta, qtaskric);
				ric = 0;
			}
			this.dispose();
		}
	}
	
	private synchronized void vendCanc(){
		boolean error = false;
		if(jTextFieldVP.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Codice non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField41.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Descrizione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField42.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jTextField43.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Descrizione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(jSpinField1.getValue() == 0){
			JOptionPane.showMessageDialog(jFrame, "Campo Quantita' non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jSpinField1.getValue() > qtacanc){
			JOptionPane.showMessageDialog(jFrame, "Quantita' non disponibile", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(error == false){
			String insert = null;
			String cbarre = jTextFieldVP.getText();
			String descrizione = jTextField41.getText();

			int qta = jSpinField1.getValue();
			//double prezzoin = Double.parseDouble(jTextField41.getText());
			double prezzout = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField42.getText()));
			double guadagno = prezzout -prezzoin;
			//Integer tot = Integer.parseInt(jTextField311.getText());
			Date d = new Date();
			Calendar c = new GregorianCalendar();
			c.setTime(d);
			int g = -1, m = -1, a = -1;
			g = c.get(Calendar.DAY_OF_MONTH);
			m = (c.get(Calendar.MONTH)) + 1;
			a = c.get(Calendar.YEAR);
			String data = String.valueOf(a)+"-"+String.valueOf(m)+"-"+String.valueOf(g);
			
			String q = "SELECT qta,data FROM vendite WHERE codice='"+cbarre+"'";
			try{
				rs = st.executeQuery(q);
				if (rs.next()){
					int nqta = rs.getInt(1)+qta;
					if (data.equals(rs.getString("data")))
					insert = "UPDATE vendite SET qta="+nqta+" WHERE codice='"+cbarre+"'";
					else insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta) VALUES('"+cbarre+"','"+descrizione+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"')";
				}
				else insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta) VALUES('"+cbarre+"','"+descrizione+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"')";
			}catch (Exception e){
				e.printStackTrace();
			}

			dbm.executeQuery(insert);
			if(ric != 0){
				update("cancelleria", "id", id, qta, qtacanc);
				ric = 0;
			}
			this.dispose();
		}
	}
	
	private synchronized void update(String tabella, String colonna, String codice, int qta1, int qta2){
		try{
			String insert;
			if(qta2 == qta1){
				JOptionPane.showMessageDialog(jFrame, "Prodotto esaurito", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				insert = "DELETE FROM "+tabella+" WHERE "+colonna+"='"+codice+"'";
				st.executeUpdate(insert);
			}
			else{
				int qta = qta2 - qta1;
				if(qta < 5)
					JOptionPane.showMessageDialog(jFrame, "Prodotti rimasti in magazzino minori di 5", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				insert = "UPDATE " + tabella + " SET qta='" + qta + "' WHERE cbarre='" + codice + "'";
				st.executeUpdate(insert);
			}
		}
		catch(Exception e46){
			e46.printStackTrace();
		}
	}
	
	private synchronized void update(String tabella, String colonna, int codice, int qta1, int qta2){
		try{
			String insert;
			if(qta2 == qta1){
				JOptionPane.showMessageDialog(jFrame, "Prodotto esaurito", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				insert = "DELETE FROM "+tabella+" WHERE "+colonna+"='"+codice+"'";
				st.executeUpdate(insert);
			}
			else{
				int qta = qta2 - qta1;
				if(qta < 5)
					JOptionPane.showMessageDialog(jFrame, "Prodotti rimasti in magazzino minori di 5", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				insert = "UPDATE " + tabella + " SET qta='" + qta + "' WHERE id='" + codice + "'";
				st.executeUpdate(insert);
			}
		}
		catch(Exception e46){
			e46.printStackTrace();
		}
	}

	/**
	 * This method initializes jSpinField	
	 * 	
	 * @return com.toedter.components.JSpinField	
	 */
	private JSpinField getJSpinField() {
		if (jSpinField == null) {
			jSpinField = new JSpinField();
			jSpinField.setBounds(new Rectangle(184, 100, 180, 22));
			jSpinField.addPropertyChangeListener("value",
					new java.beans.PropertyChangeListener() {
						public void propertyChange(java.beans.PropertyChangeEvent e) {
							System.out.println("propertyChange(value)"); // TODO Auto-generated property Event stub "value"
							if(jSpinField.getValue() != 0){
								double d = (Double.parseDouble(jTextField5.getText().trim()))*(jSpinField.getValue()); 
								jTextField8.setText(String.valueOf(d));
							}
						}
					});
		}
		return jSpinField;
	}

	/**
	 * This method initializes jSpinField1	
	 * 	
	 * @return com.toedter.components.JSpinField	
	 */
	private JSpinField getJSpinField1() {
		if (jSpinField1 == null) {
			jSpinField1 = new JSpinField();
			jSpinField1.setBounds(new Rectangle(184, 100, 180, 22));
			jSpinField1.addPropertyChangeListener("value",
					new java.beans.PropertyChangeListener() {
						public void propertyChange(java.beans.PropertyChangeEvent e) {
							System.out.println("propertyChange(value)"); // TODO Auto-generated property Event stub "value"
							if(jSpinField1.getValue() != 0){
								double d = (Double.parseDouble(jTextField42.getText().trim()))*(jSpinField1.getValue()); 
								jTextField311.setText(String.valueOf(d));
							}
						}
					});
		}
		return jSpinField1;
	}

	/**
	 * This method initializes jTextFieldNT	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldNT() {
		if (jTextFieldNT == null) {
			jTextFieldNT = new JTextField();
			jTextFieldNT.setBounds(new Rectangle(184, 29, 180, 22));
			/*jTextFieldNT.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						aggiornaCombo();
					}
				}
			});*/
		}
		return jTextFieldNT;
	}

	/**
	 * This method initializes jButtonCercaTN	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercaTN() {
		if (jButtonCercaTN == null) {
			jButtonCercaTN = new JButton();
			jButtonCercaTN.setBounds(new Rectangle(375, 25, 75, 29));
			jButtonCercaTN.setText("Cerca");
			jButtonCercaTN.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					aggiornaCombo();
				}
			});
		}
		return jButtonCercaTN;
	}

	/**
	 * This method initializes jTextFieldVT	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldVT() {
		if (jTextFieldVT == null) {
			jTextFieldVT = new JTextField();
			jTextFieldVT.setBounds(new Rectangle(184, 29, 180, 22));
			/*jTextFieldVT.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						aggiornaCombo1();
					}
				}
			});*/
		}
		return jTextFieldVT;
	}

	/**
	 * This method initializes jButtonTV	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonTV() {
		if (jButtonTV == null) {
			jButtonTV = new JButton();
			jButtonTV.setBounds(new Rectangle(375, 25, 75, 29));
			jButtonTV.setText("Cerca");
			jButtonTV.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					aggiornaCombo1();
				}
			});
		}
		return jButtonTV;
	}

	/**
	 * This method initializes jTextFieldSR	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldSR() {
		if (jTextFieldSR == null) {
			jTextFieldSR = new JTextField();
			jTextFieldSR.setBounds(new Rectangle(184, 34, 180, 22));
			/*jTextFieldSR.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						cercaSR();
					}
				}
			});*/
		}
		return jTextFieldSR;
	}

	/**
	 * This method initializes jButtonCercaSR	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercaSR() {
		if (jButtonCercaSR == null) {
			jButtonCercaSR = new JButton();
			jButtonCercaSR.setBounds(new Rectangle(375, 31, 75, 28));
			jButtonCercaSR.setText("Cerca");
			jButtonCercaSR.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					cercaSR();
				}
			});
		}
		return jButtonCercaSR;
	}

	/**
	 * This method initializes jTextFieldVP	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldVP() {
		if (jTextFieldVP == null) {
			jTextFieldVP = new JTextField();
			jTextFieldVP.setBounds(new Rectangle(184, 34, 180, 22));
			/*jTextFieldVP.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						cercaC();
					}
				}
			});*/ 
		}
		return jTextFieldVP;
	}

	/**
	 * This method initializes jButtonCercaP	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercaP() {
		if (jButtonCercaP == null) {
			jButtonCercaP = new JButton();
			jButtonCercaP.setBounds(new Rectangle(375, 31, 75, 28));
			jButtonCercaP.setText("Cerca");
			jButtonCercaP.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					cercaC();
				}
			});
		}
		return jButtonCercaP;
	}

	/**
	 * This method initializes jButtonStampa	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStampa() {
		if (jButtonStampa == null) {
			jButtonStampa = new JButton();
			jButtonStampa.setBounds(new Rectangle(365, 200, 135, 29));
			jButtonStampa.setText("Stampa Attestato");
			jButtonStampa.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
						stampa();
						String query3 = "DELETE FROM stampa";
						dbm.executeQuery(query3);
					//}
				}
			});
		}
		return jButtonStampa;
	}

	/**
	 * This method initializes jButtonStampa2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStampa2() {
		if (jButtonStampa2 == null) {
			jButtonStampa2 = new JButton();
			jButtonStampa2.setBounds(new Rectangle(365, 200, 135, 29));
			jButtonStampa2.setText("Stampa Attestato");
			jButtonStampa2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
						stampa1();
						String query3 = "DELETE FROM stampa";
						dbm.executeQuery(query3);
					//}
				}
			});
		}
		return jButtonStampa2;
	}

}
